package com.lagou.Title2;

import java.io.File;

public class DeleteALL {

    public static void main(String[] args) {
        File file = new File("d:/目录");
        delAllFile(file);
    }

    /**
     * 删除工作采用递归的思想
     *
     */
    public static void delAllFile(File directory) {
        if (!directory.isDirectory()) {
            directory.delete();
        } else {
            File[] files = directory.listFiles();

            // 空目录
            if (files.length == 0) {
                directory.delete();
                System.out.println("删除" + directory.getAbsolutePath());
                return;
            }

            // 遍历

            for (File file : files) {
                if (file.isDirectory()) {
                    delAllFile(file);
                } else {
                    file.delete();
                    System.out.println("删除" + file.getAbsolutePath());
                }
            }

            // 删除目录本身
            directory.delete();
            System.out.println("删除" + directory.getAbsolutePath());
        }
    }
}

